package com.xxxx.car.appointment.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxxx.car.appointment.vo.ServiceItemAuditInfo;
import com.xxxx.car.appointment.vo.ServiceItemVo;
import com.xxxx.car.common.core.domain.entity.SysUser;
import com.xxxx.car.common.utils.DateUtils;
import com.xxxx.car.common.utils.StringUtils;
import com.xxxx.car.flow.service.IBpmnInfoService;
import com.xxxx.car.system.service.ISysUserService;
import io.jsonwebtoken.lang.Assert;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.xxxx.car.appointment.mapper.ServiceItemMapper;
import com.xxxx.car.appointment.domain.ServiceItem;
import com.xxxx.car.appointment.service.IServiceItemService;
import static com.xxxx.car.appointment.enums.ServiceItemAuditStatusEnum.*;

import javax.annotation.Resource;

/**
 * 静态导入: import static => 允许将指定类中的指定公共成员导入到当前类,在当前类中使用这些成员时,就如同本地成员的使用方式
 *
 * 汽车服务项Service业务层处理
 * 
 * @author Huang
 * @date 2024-08-26
 */
@Service
public class ServiceItemServiceImpl extends ServiceImpl<ServiceItemMapper,ServiceItem> implements IServiceItemService
{
    @Resource
    private ServiceItemMapper serviceItemMapper;
    @Resource
    private ISysUserService sysUserService;
    @Resource
    private IBpmnInfoService bpmnInfoService;

    /**
     * 查询汽车服务项
     * 
     * @param id 汽车服务项主键
     * @return 汽车服务项
     */
    @Override
    public ServiceItem selectServiceItemById(Long id)
    {
        return serviceItemMapper.selectServiceItemById(id);
    }

    /**
     * 查询汽车服务项列表
     * 
     * @param serviceItem 汽车服务项
     * @return 汽车服务项
     */
    @Override
    public List<ServiceItem> selectServiceItemList(ServiceItem serviceItem)
    {
        return serviceItemMapper.selectServiceItemList(serviceItem);
    }

    /**
     * 新增汽车服务项
     * 
     * @param serviceItemVo 汽车服务项
     * @return 结果
     */
    @Override
    public int insertServiceItem(ServiceItemVo serviceItemVo)
    {
        Assert.state(serviceItemVo.getDiscountPrice().compareTo(serviceItemVo.getOriginalPrice())<=0,"服务折扣价不能大于原价");
        ServiceItem serviceItem = new ServiceItem();
        BeanUtils.copyProperties(serviceItemVo,serviceItem);
        serviceItem.setCreateTime(DateUtils.getNowDate());
        // 不是套餐 无需审核
        if(ServiceItemVo.NOT_MEAL.equals(serviceItemVo.getIsMeal())){
            serviceItem.setAuditStatus((long) NONE.ordinal());
        }else {
            serviceItem.setAuditStatus((long) INITIALING.ordinal());
        }
        serviceItem.setSaleStatus(ServiceItemVo.SALE_STATUS_NO);
        return serviceItemMapper.insertServiceItem(serviceItem);
    }

    /**
     * 修改汽车服务项
     * 
     * @param serviceItemVo 汽车服务项
     * @return 结果
     */
    @Override
    public int updateServiceItem(ServiceItemVo serviceItemVo)
    {
        ServiceItem serviceItem = serviceItemMapper.selectServiceItemById(serviceItemVo.getId());
        Assert.state(serviceItem.getAuditStatus() != AUDITING.ordinal()
                || !Objects.equals(serviceItem.getSaleStatus(), ServiceItemVo.SALE_STATUS_YES),"服务项已上架或者审核中不允许编辑");
        Assert.notNull(serviceItem,"参数错误!");
        BeanUtils.copyProperties(serviceItemVo,serviceItem);
        if(serviceItem.getAuditStatus() == PASSED.ordinal() ||
                NONE.ordinal() == serviceItem.getAuditStatus() ||
                INITIALING.ordinal() == serviceItem.getAuditStatus()){
            if(Objects.equals(serviceItemVo.getIsMeal(), ServiceItemVo.IS_MEAL)){
                serviceItem.setAuditStatus((long) INITIALING.ordinal());
            }else {
                serviceItem.setAuditStatus((long) NONE.ordinal());
            }
        }
        return serviceItemMapper.updateServiceItem(serviceItem);
    }

    /**
     * 批量删除汽车服务项
     * 
     * @param ids 需要删除的汽车服务项主键
     * @return 结果
     */
    @Override
    public int deleteServiceItemByIds(Long[] ids)
    {
        return serviceItemMapper.deleteServiceItemByIds(ids);
    }

    /**
     * 删除汽车服务项信息
     * 
     * @param id 汽车服务项主键
     * @return 结果
     */
    @Override
    public int deleteServiceItemById(Long id)
    {
        return serviceItemMapper.deleteServiceItemById(id);
    }

    /**
     *  1.上架：如果是套餐 那么需要审核通过才能上架。如果不是套餐则可以正常上架
     *  2.下架：如果是套餐,上架了都是审核通过的,下架只是更改状态,未修改审核通过的,审核状态无需改变.
     */
    @Override
    public int updateSaleStatus(Long id) {
        ServiceItem serviceItem = serviceItemMapper.selectServiceItemById(id);
        Assert.notNull(serviceItem,"参数错误!");

        // 套餐才需要 ---- 首先判断是否为套餐
        if(Objects.equals(serviceItem.getIsMeal(), ServiceItemVo.IS_MEAL)){
            Assert.state(serviceItem.getAuditStatus() == PASSED.ordinal(),"只有审核通过才能上架");
        }

        // 更改状态
        Long saleStatus = Objects.equals(serviceItem.getSaleStatus(), ServiceItemVo.SALE_STATUS_NO) ? ServiceItemVo.SALE_STATUS_YES : ServiceItemVo.SALE_STATUS_NO;

        return serviceItemMapper.updateSaleStatus(id,saleStatus);
    }

    @Override
    public ServiceItemAuditInfo selectAuditInfoById(Long id) {
        ServiceItemAuditInfo info = new ServiceItemAuditInfo();
        // 1.查询服务项信息
        ServiceItem serviceItem = this.selectServiceItemById(id);
        info.setServiceItem(serviceItem);
        // 2.查询流程定义中的折扣价 TODO
        BigDecimal discountPrice = bpmnInfoService.getCarPackageAuditDiscountPrice();
        info.setLimitDiscountPrice(discountPrice);

        // 3.查询店长列表
        List<SysUser> showOwners = sysUserService.selectByRoleKey("shopOwner");
        Assert.state(showOwners.size() > 0,"请先添加店长");
        info.setShopOwners(showOwners);
        // 4.根据流程定义的限制折扣价,判断是否需要查询财务列表
        if(serviceItem.getDiscountPrice().compareTo(discountPrice) >= 0){
            // 5.如果需要,查询财务列表
            List<SysUser> financialList = sysUserService.selectByRoleKey("financial");
            Assert.state(financialList.size() > 0,"请先添加财务");
            info.setFinances(financialList);
        }
        // 6.封装完整对象,返回
        return info;
    }
}
